<?php
# http://projects:8081/cdapp-2kt-api/api/flow_sector_preset.php

include_once '_portal.php';

if (isset($_GET['delete'])) {
  $delete = $_GET['delete'];
  $sql = "DELETE FROM `2kt_flow_preset` WHERE `number` = {$delete}";
  $result = exec_sql($sql);
  $sql1 = "SELECT * FROM `2kt_flow_preset`";
  $content = exec_sql($sql1);
  if ($content) {
    $content = query_parse($content);
  } else {
    $content = [];
  }
  output(['result' => $result, 'content' => $content, 'sql' => $sql]);
} elseif (isset($post)) {
  $number = $post->number;
  $title = $post->title;
  $description = $post->description;
  $type = '自定义';
  $content = json_stringify($post->content);
  if ($number == null) {
    $sql = "INSERT INTO `2kt_flow_preset`(`number`, `title`, `description`, `type`, `content`) VALUES (null,'{$title}','{$description}','{$type}','{$content}')";
    $result = exec_sql($sql);
    $sql = "SELECT * FROM `2kt_flow_preset`";
    $content = exec_sql($sql);
    if ($content) {
      $content = query_parse($content);
    } else {
      $content = [];
    }
    output(['result' => $result, 'content' => $content]);
  } else {
    $sql = "UPDATE `2kt_flow_preset` SET `title`='{$title}',`description`='{$description}',`content`='{$content}' WHERE `number` = {$number}";
    $result = exec_sql($sql);
    $sql = "SELECT * FROM `2kt_flow_preset`";
    $content = exec_sql($sql);
    if ($content) {
      $content = query_parse($content);
    } else {
      $content = [];
    }
    output(['result' => $result, 'content' => $content]);
  }
} else {
  $sql = "SELECT * FROM `2kt_flow_preset`";
  $content = exec_sql($sql);
  $result = true;
  if ($content) {
    $content = query_parse($content);
  } else {
    $result = false;
    $content = [];
  }
  output(['result' => $result, 'content' => $content]);
}
